const Chunk = require("../template")

function testChunkAdvancedMethods() {
  console.log(
    "1. 带步长的分块测试:",
    (() => {
      const arr = [1, 2, 3, 4, 5, 6, 7, 8]
      const result = Chunk.chunkWithStep(arr, 3, 2)
      const expected = [[1, 2, 3], [3, 4, 5], [5, 6, 7], [7, 8]]
      return JSON.stringify(result) === JSON.stringify(expected)
    })()
  )

  console.log(
    "2. 带填充的分块测试:",
    (() => {
      const arr = [1, 2, 3, 4, 5]
      const result = Chunk.chunkWithFill(arr, 3, 0)
      const expected = [[1, 2, 3], [4, 5, 0]]
      return JSON.stringify(result) === JSON.stringify(expected)
    })()
  )

  console.log(
    "3. 带过滤的分块测试:",
    (() => {
      const arr = [1, 2, 3, 4, 5, 6, 7, 8]
      const result = Chunk.chunkWithFilter(arr, 3, (item) => item % 2 === 0)
      const expected = [[2, 4, 6], [8]]
      return JSON.stringify(result) === JSON.stringify(expected)
    })()
  )

  console.log(
    "4. 带映射的分块测试:",
    (() => {
      const arr = [1, 2, 3, 4, 5]
      const result = Chunk.chunkWithMap(arr, 2, (item) => item * 2)
      const expected = [[2, 4], [6, 8], [10]]
      return JSON.stringify(result) === JSON.stringify(expected)
    })()
  )

  console.log(
    "5. 异步分块测试:",
    (() => {
      const arr = [1, 2, 3, 4, 5]
      const processor = async (chunk) => chunk.map(item => item * 2)
      
      return new Promise((resolve) => {
        Chunk.chunkAsync(arr, 2, processor).then((result) => {
          const expected = [[2, 4], [6, 8], [10]]
          resolve(JSON.stringify(result) === JSON.stringify(expected))
        })
      })
    })()
  )

  console.log(
    "6. 并发分块测试:",
    (() => {
      const arr = [1, 2, 3, 4, 5, 6, 7, 8]
      const processor = async (chunk) => chunk.map(item => item * 2)
      
      return new Promise((resolve) => {
        Chunk.chunkConcurrent(arr, 2, processor, 2).then((result) => {
          const expected = [[2, 4], [6, 8], [10, 12], [14, 16]]
          resolve(JSON.stringify(result) === JSON.stringify(expected))
        })
      })
    })()
  )

  console.log(
    "7. 带错误处理的分块测试:",
    (() => {
      const arr = [1, 2, 3, 4, 5]
      const processor = (chunk) => {
        if (chunk.includes(3)) {
          throw new Error('Test error')
        }
        return chunk.map(item => item * 2)
      }
      const errorHandler = (error, chunk) => {
        return chunk.map(item => item * 3)
      }
      
      const result = Chunk.chunkWithErrorHandling(arr, 2, processor, errorHandler)
      const expected = [[2, 4], [9, 12], [15]]
      return JSON.stringify(result) === JSON.stringify(expected)
    })()
  )

  console.log(
    "8. 带进度回调的分块测试:",
    (() => {
      const arr = [1, 2, 3, 4, 5, 6]
      let progressCount = 0
      const processor = (chunk) => chunk.map(item => item * 2)
      const progressCallback = (current, total) => {
        progressCount++
      }
      
      const result = Chunk.chunkWithProgress(arr, 2, processor, progressCallback)
      const expected = [[2, 4], [6, 8], [10, 12]]
      
      return JSON.stringify(result) === JSON.stringify(expected) && progressCount === 3
    })()
  )

  console.log(
    "9. 可取消的分块测试:",
    (() => {
      const arr = [1, 2, 3, 4, 5, 6, 7, 8]
      const processor = (chunk) => chunk.map(item => item * 2)
      const controller = { cancelled: false }
      
      const result = Chunk.chunkWithCancel(arr, 2, processor, controller)
      const expected = [[2, 4], [6, 8], [10, 12], [14, 16]]
      
      return JSON.stringify(result) === JSON.stringify(expected)
    })()
  )

  console.log(
    "10. 复杂对象分块测试:",
    (() => {
      const arr = [
        { id: 1, name: 'A', value: 10 },
        { id: 2, name: 'B', value: 20 },
        { id: 3, name: 'C', value: 30 },
        { id: 4, name: 'D', value: 40 },
        { id: 5, name: 'E', value: 50 }
      ]
      const result = Chunk.chunkWithMap(arr, 2, (item) => ({
        ...item,
        value: item.value * 2
      }))
      
      return result.length === 3 &&
             result[0].length === 2 &&
             result[1].length === 2 &&
             result[2].length === 1 &&
             result[0][0].value === 20
    })()
  )

  console.log(
    "11. this上下文高级方法测试:",
    (() => {
      const advancedObj = {
        multiplier: 2,
        processAdvanced: function(arr) {
          const chunks = Chunk.chunkWithMap(arr, 2, (item) => item * this.multiplier)
          return chunks.length
        }
      }
      const arr = [1, 2, 3, 4, 5]
      return advancedObj.processAdvanced(arr) === 3
    })()
  )

  console.log(
    "12. 复杂对象this绑定高级方法测试:",
    (() => {
      const advancedConfigObj = {
        config: { threshold: 2 },
        processWithConfig: function(arr) {
          const chunks = Chunk.chunkWithFilter(arr, 2, (item) => item >= this.config.threshold)
          return chunks.length > 0
        }
      }
      const arr = [1, 2, 3, 4, 5]
      return advancedConfigObj.processWithConfig(arr) === true
    })()
  )
}

testChunkAdvancedMethods()
